import Frames.Circle;
import Frames.Cursor;
import IO.BeatMapReader;
import javazoom.jl.decoder.JavaLayerException;
import musics.MusicPlayer;

import java.io.FileNotFoundException;
import java.io.IOException;

public class Runner {
    public String[] objects;
    public Circle[] hitObjects ;
    public Runner(String song) throws IOException, JavaLayerException {
        BeatMapReader fr = new BeatMapReader("beatmap.osu");
        String text = fr.read();
        String ss = fr.getMusicPath();
        objects = text.split("\n");
        hitObjects = new Circle[getTime(objects[objects.length-1])];
        //初始化数组（放Circle的）
        for(int i = 0;i<objects.length;i++){
            String s = objects[i];
            try {
                System.out.println(getTime(s));
                hitObjects[getTime(s)-500] = new Circle(getX(s), getY(s),i,getTime(objects[i+1])-getTime(s));
            }catch(IndexOutOfBoundsException e){
                hitObjects[getTime(s)-1] = new Circle(getX(s), getY(s),i,70);
            }
        }
        System.out.println("初始化完成，共"+hitObjects.length+"ms");

        Thread t = new Thread(){
            public void run(){  //核心部分，用于使Circle出现的
                long time1 = System.currentTimeMillis();
                long lastTime = time1+hitObjects.length;
                while(System.currentTimeMillis()<lastTime-1){
                    try {
                        hitObjects[(int) (System.currentTimeMillis()-time1)].setVisible(true);
                    }catch(NullPointerException e){

                    }
                }
            }
        };
        t.start();
        MusicPlayer mp = new MusicPlayer("audio.mp3");
        mp.run();
    }
    public int getX(String objects){
        String[] s = objects.split(",");
        return Integer.valueOf(s[0]);
    }
    public int getY(String objects){
        String[] s = objects.split(",");
        return Integer.valueOf(s[1]);
    }
    public int getTime(String objects){
        String[] s = objects.split(",");
        return Integer.valueOf(s[2]);
    }
}
